<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>ierange.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_browserrange_ierange.js.html">ierange.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Definition of the IE browser specific range wrapper.
<a name="line17"></a> *
<a name="line18"></a> * DO NOT USE THIS FILE DIRECTLY.  Use goog.dom.Range instead.
<a name="line19"></a> *
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.dom.browserrange.IeRange&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.array&#39;);
<a name="line26"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom&#39;);
<a name="line28"></a>goog.require(&#39;goog.dom.NodeIterator&#39;);
<a name="line29"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line30"></a>goog.require(&#39;goog.dom.RangeEndpoint&#39;);
<a name="line31"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line32"></a>goog.require(&#39;goog.dom.browserrange.AbstractRange&#39;);
<a name="line33"></a>goog.require(&#39;goog.iter&#39;);
<a name="line34"></a>goog.require(&#39;goog.iter.StopIteration&#39;);
<a name="line35"></a>goog.require(&#39;goog.string&#39;);
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>/**
<a name="line39"></a> * The constructor for IE specific browser ranges.
<a name="line40"></a> * @param {TextRange} range The range object.
<a name="line41"></a> * @param {Document} doc The document the range exists in.
<a name="line42"></a> * @constructor
<a name="line43"></a> * @extends {goog.dom.browserrange.AbstractRange}
<a name="line44"></a> */
<a name="line45"></a>goog.dom.browserrange.IeRange = function(range, doc) {
<a name="line46"></a>  /**
<a name="line47"></a>   * The browser range object this class wraps.
<a name="line48"></a>   * @type {TextRange}
<a name="line49"></a>   * @private
<a name="line50"></a>   */
<a name="line51"></a>  this.range_ = range;
<a name="line52"></a>
<a name="line53"></a>  /**
<a name="line54"></a>   * The document the range exists in.
<a name="line55"></a>   * @type {Document}
<a name="line56"></a>   * @private
<a name="line57"></a>   */
<a name="line58"></a>  this.doc_ = doc;
<a name="line59"></a>};
<a name="line60"></a>goog.inherits(goog.dom.browserrange.IeRange,
<a name="line61"></a>    goog.dom.browserrange.AbstractRange);
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>/**
<a name="line66"></a> * Logging object.
<a name="line67"></a> * @type {goog.debug.Logger}
<a name="line68"></a> * @private
<a name="line69"></a> */
<a name="line70"></a>goog.dom.browserrange.IeRange.logger_ =
<a name="line71"></a>    goog.debug.Logger.getLogger(&#39;goog.dom.browserrange.IeRange&#39;);
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * Returns a browser range spanning the given node&#39;s contents.
<a name="line76"></a> * @param {Node} node The node to select.
<a name="line77"></a> * @return {TextRange} A browser range spanning the node&#39;s contents.
<a name="line78"></a> * @private
<a name="line79"></a> */
<a name="line80"></a>goog.dom.browserrange.IeRange.getBrowserRangeForNode_ = function(node) {
<a name="line81"></a>  var nodeRange = goog.dom.getOwnerDocument(node).body.createTextRange();
<a name="line82"></a>  if (node.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line83"></a>    // Elements are easy.
<a name="line84"></a>    nodeRange.moveToElementText(node);
<a name="line85"></a>  } else {
<a name="line86"></a>    // Text nodes are hard.
<a name="line87"></a>    // Compute the offset from the nearest element related position.
<a name="line88"></a>    var offset = 0;
<a name="line89"></a>    var sibling = node;
<a name="line90"></a>    while (sibling = sibling.previousSibling) {
<a name="line91"></a>      var nodeType = sibling.nodeType;
<a name="line92"></a>      if (nodeType == goog.dom.NodeType.TEXT) {
<a name="line93"></a>        offset += sibling.length;
<a name="line94"></a>      } else if (nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line95"></a>        // Move to the space after this element.
<a name="line96"></a>        nodeRange.moveToElementText(sibling);
<a name="line97"></a>        break;
<a name="line98"></a>      }
<a name="line99"></a>    }
<a name="line100"></a>
<a name="line101"></a>    if (!sibling) {
<a name="line102"></a>      nodeRange.moveToElementText(node.parentNode);
<a name="line103"></a>    }
<a name="line104"></a>
<a name="line105"></a>    nodeRange.collapse(!sibling);
<a name="line106"></a>
<a name="line107"></a>    if (offset) {
<a name="line108"></a>      nodeRange.move(&#39;character&#39;, offset);
<a name="line109"></a>    }
<a name="line110"></a>
<a name="line111"></a>    nodeRange.moveEnd(&#39;character&#39;, node.length);
<a name="line112"></a>  }
<a name="line113"></a>
<a name="line114"></a>  return nodeRange;
<a name="line115"></a>};
<a name="line116"></a>
<a name="line117"></a>
<a name="line118"></a>/**
<a name="line119"></a> * Returns a browser range spanning the given nodes.
<a name="line120"></a> * @param {Node} startNode The node to start with.
<a name="line121"></a> * @param {number} startOffset The offset within the start node.
<a name="line122"></a> * @param {Node} endNode The node to end with.
<a name="line123"></a> * @param {number} endOffset The offset within the end node.
<a name="line124"></a> * @return {TextRange} A browser range spanning the node&#39;s contents.
<a name="line125"></a> * @private
<a name="line126"></a> */
<a name="line127"></a>goog.dom.browserrange.IeRange.getBrowserRangeForNodes_ = function(startNode,
<a name="line128"></a>    startOffset, endNode, endOffset) {
<a name="line129"></a>  // Create a range starting at the correct start position.
<a name="line130"></a>  var child, collapse = false;
<a name="line131"></a>  if (startNode.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line132"></a>    if (startOffset &gt; startNode.childNodes.length) {
<a name="line133"></a>      goog.dom.browserrange.IeRange.logger_.severe(
<a name="line134"></a>          &#39;Cannot have startOffset &gt; startNode child count&#39;);
<a name="line135"></a>    }
<a name="line136"></a>    child = startNode.childNodes[startOffset];
<a name="line137"></a>    collapse = !child;
<a name="line138"></a>    startNode = child || startNode;
<a name="line139"></a>    startOffset = 0;
<a name="line140"></a>  }
<a name="line141"></a>  var leftRange = goog.dom.browserrange.IeRange.
<a name="line142"></a>      getBrowserRangeForNode_(startNode);
<a name="line143"></a>  if (startOffset) {
<a name="line144"></a>    leftRange.move(&#39;character&#39;, startOffset);
<a name="line145"></a>  }
<a name="line146"></a>  if (collapse) {
<a name="line147"></a>    leftRange.collapse(false);
<a name="line148"></a>  }
<a name="line149"></a>
<a name="line150"></a>  // Create a range that ends at the right position.
<a name="line151"></a>  collapse = false;
<a name="line152"></a>  if (endNode.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line153"></a>    if (startOffset &gt; startNode.childNodes.length) {
<a name="line154"></a>      goog.dom.browserrange.IeRange.logger_.severe(
<a name="line155"></a>          &#39;Cannot have endOffset &gt; endNode child count&#39;);
<a name="line156"></a>    }
<a name="line157"></a>    child = endNode.childNodes[endOffset];
<a name="line158"></a>    endNode = child || endNode;
<a name="line159"></a>    endOffset = 0;
<a name="line160"></a>    collapse = !child;
<a name="line161"></a>  }
<a name="line162"></a>  var rightRange = goog.dom.browserrange.IeRange.
<a name="line163"></a>      getBrowserRangeForNode_(endNode);
<a name="line164"></a>  rightRange.collapse(!collapse);
<a name="line165"></a>  if (endOffset) {
<a name="line166"></a>    rightRange.moveEnd(&#39;character&#39;, endOffset);
<a name="line167"></a>  }
<a name="line168"></a>
<a name="line169"></a>  // Merge and return.
<a name="line170"></a>  leftRange.setEndPoint(&#39;EndToEnd&#39;, rightRange);
<a name="line171"></a>  return leftRange;
<a name="line172"></a>};
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>/**
<a name="line176"></a> * Create a range object that selects the given node&#39;s text.
<a name="line177"></a> * @param {Node} node The node to select.
<a name="line178"></a> * @return {goog.dom.browserrange.IeRange} An IE range wrapper object.
<a name="line179"></a> */
<a name="line180"></a>goog.dom.browserrange.IeRange.createFromNodeContents = function(node) {
<a name="line181"></a>  var range = new goog.dom.browserrange.IeRange(
<a name="line182"></a>      goog.dom.browserrange.IeRange.getBrowserRangeForNode_(node),
<a name="line183"></a>      goog.dom.getOwnerDocument(node));
<a name="line184"></a>  range.parentNode_ = node;
<a name="line185"></a>  return range;
<a name="line186"></a>};
<a name="line187"></a>
<a name="line188"></a>
<a name="line189"></a>/**
<a name="line190"></a> * Static method that returns the proper type of browser range.
<a name="line191"></a> * @param {Node} startNode The node to start with.
<a name="line192"></a> * @param {number} startOffset The offset within the start node.
<a name="line193"></a> * @param {Node} endNode The node to end with.
<a name="line194"></a> * @param {number} endOffset The offset within the end node.
<a name="line195"></a> * @return {goog.dom.browserrange.AbstractRange} A wrapper object.
<a name="line196"></a> */
<a name="line197"></a>goog.dom.browserrange.IeRange.createFromNodes = function(startNode,
<a name="line198"></a>    startOffset, endNode, endOffset) {
<a name="line199"></a>  return new goog.dom.browserrange.IeRange(
<a name="line200"></a>      goog.dom.browserrange.IeRange.getBrowserRangeForNodes_(startNode,
<a name="line201"></a>          startOffset, endNode, endOffset),
<a name="line202"></a>      goog.dom.getOwnerDocument(startNode));
<a name="line203"></a>};
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>// Even though goog.dom.TextRange does similar caching to below, keeping these
<a name="line207"></a>// caches allows for better performance in the get*Offset methods.
<a name="line208"></a>
<a name="line209"></a>
<a name="line210"></a>/**
<a name="line211"></a> * Lazy cache of the node containing the entire selection.
<a name="line212"></a> * @type {Node?}
<a name="line213"></a> * @private
<a name="line214"></a> */
<a name="line215"></a>goog.dom.browserrange.IeRange.prototype.parentNode_ = null;
<a name="line216"></a>
<a name="line217"></a>
<a name="line218"></a>/**
<a name="line219"></a> * Lazy cache of the node containing the start of the selection.
<a name="line220"></a> * @type {Node?}
<a name="line221"></a> * @private
<a name="line222"></a> */
<a name="line223"></a>goog.dom.browserrange.IeRange.prototype.startNode_ = null;
<a name="line224"></a>
<a name="line225"></a>
<a name="line226"></a>/**
<a name="line227"></a> * Lazy cache of the node containing the end of the selection.
<a name="line228"></a> * @type {Node?}
<a name="line229"></a> * @private
<a name="line230"></a> */
<a name="line231"></a>goog.dom.browserrange.IeRange.prototype.endNode_ = null;
<a name="line232"></a>
<a name="line233"></a>
<a name="line234"></a>
<a name="line235"></a>/**
<a name="line236"></a> * @return {goog.dom.browserrange.IeRange} A clone of this range.
<a name="line237"></a> */
<a name="line238"></a>goog.dom.browserrange.IeRange.prototype.clone = function() {
<a name="line239"></a>  var range = new goog.dom.browserrange.IeRange(
<a name="line240"></a>      this.range_.duplicate(), this.doc_);
<a name="line241"></a>  range.parentNode_ = this.parentNode_;
<a name="line242"></a>  range.startNode_ = this.startNode_;
<a name="line243"></a>  range.endNode_ = this.endNode_;
<a name="line244"></a>  return range;
<a name="line245"></a>};
<a name="line246"></a>
<a name="line247"></a>
<a name="line248"></a>/** @inheritDoc */
<a name="line249"></a>goog.dom.browserrange.IeRange.prototype.getBrowserRange = function() {
<a name="line250"></a>  return this.range_;
<a name="line251"></a>};
<a name="line252"></a>
<a name="line253"></a>
<a name="line254"></a>/**
<a name="line255"></a> * Clears the cached values for containers.
<a name="line256"></a> * @private
<a name="line257"></a> */
<a name="line258"></a>goog.dom.browserrange.IeRange.prototype.clearCachedValues_ = function() {
<a name="line259"></a>  this.parentNode_ = this.startNode_ = this.endNode_ = null;
<a name="line260"></a>};
<a name="line261"></a>
<a name="line262"></a>
<a name="line263"></a>/** @inheritDoc */
<a name="line264"></a>goog.dom.browserrange.IeRange.prototype.getContainer = function() {
<a name="line265"></a>  if (!this.parentNode_) {
<a name="line266"></a>    var selectText = this.range_.text;
<a name="line267"></a>
<a name="line268"></a>    // If the selection ends with spaces, we need to remove these to get the
<a name="line269"></a>    // parent container of only the real contents.  This is to get around IE&#39;s
<a name="line270"></a>    // inconsistency where it selects the spaces after a word when you double
<a name="line271"></a>    // click, but leaves out the spaces during execCommands.
<a name="line272"></a>    // NOTE: this relies on the fact that &#39;foo&#39;.charAt(-1) == &#39;&#39;
<a name="line273"></a>    for (var i = 1; selectText.charAt(selectText.length - i) == &#39; &#39;; i++) {
<a name="line274"></a>      this.range_.moveEnd(&#39;character&#39;, -1);
<a name="line275"></a>    }
<a name="line276"></a>
<a name="line277"></a>    // Get the parent node.  This should be the end, but alas, it is not.
<a name="line278"></a>    var parent = this.range_.parentElement();
<a name="line279"></a>
<a name="line280"></a>    // Deal with selection bug where IE thinks one of the selection&#39;s children
<a name="line281"></a>    // is actually the selection&#39;s parent. Relies on the assumption that the
<a name="line282"></a>    // HTML text of the parent container is longer than the length of the
<a name="line283"></a>    // selection&#39;s HTML text.
<a name="line284"></a>
<a name="line285"></a>    // Also note IE will sometimes insert \r and \n whitespace, which should be
<a name="line286"></a>    // disregarded. Otherwise the loop may run too long and return wrong parent
<a name="line287"></a>    var htmlText = goog.string.stripNewlines(this.range_.htmlText);
<a name="line288"></a>    while (htmlText.length &gt;
<a name="line289"></a>           goog.string.stripNewlines(parent.outerHTML).length) {
<a name="line290"></a>      parent = parent.parentNode;
<a name="line291"></a>    }
<a name="line292"></a>
<a name="line293"></a>    // Deal with IE&#39;s selecting the outer tags when you double click
<a name="line294"></a>    // If the innerText is the same, then we just want the inner node
<a name="line295"></a>    while (parent.childNodes.length == 1 &amp;&amp;
<a name="line296"></a>           parent.innerText == goog.dom.browserrange.IeRange.getNodeText_(
<a name="line297"></a>               parent.firstChild)) {
<a name="line298"></a>      // It doesn&#39;t make sense for an image to be returned as a container,
<a name="line299"></a>      // so check if the child is an image and return the parent instead.
<a name="line300"></a>      if (parent.firstChild.tagName == &#39;IMG&#39;) {
<a name="line301"></a>        break;
<a name="line302"></a>      }
<a name="line303"></a>      parent = parent.firstChild;
<a name="line304"></a>    }
<a name="line305"></a>
<a name="line306"></a>    // If the selection is empty, we may need to do extra work to position it
<a name="line307"></a>    // properly.
<a name="line308"></a>    if (selectText.length == 0) {
<a name="line309"></a>      parent = this.findDeepestContainer_(parent);
<a name="line310"></a>    }
<a name="line311"></a>
<a name="line312"></a>    this.parentNode_ = parent;
<a name="line313"></a>  }
<a name="line314"></a>
<a name="line315"></a>  return this.parentNode_;
<a name="line316"></a>};
<a name="line317"></a>
<a name="line318"></a>
<a name="line319"></a>/**
<a name="line320"></a> * Helper method to find the deepest parent for this range, starting
<a name="line321"></a> * the search from {@code node}, which must contain the range.
<a name="line322"></a> * @param {Node} node The node to start the search from.
<a name="line323"></a> * @return {Node} The deepest parent for this range.
<a name="line324"></a> * @private
<a name="line325"></a> */
<a name="line326"></a>goog.dom.browserrange.IeRange.prototype.findDeepestContainer_ = function(node) {
<a name="line327"></a>  var childNodes = node.childNodes;
<a name="line328"></a>  for (var i = 0, len = childNodes.length; i &lt; len; i++) {
<a name="line329"></a>    var child = childNodes[i];
<a name="line330"></a>
<a name="line331"></a>    if (child.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line332"></a>      if (this.range_.inRange(
<a name="line333"></a>          goog.dom.browserrange.IeRange.getBrowserRangeForNode_(child))) {
<a name="line334"></a>        return this.findDeepestContainer_(child);
<a name="line335"></a>      }
<a name="line336"></a>    }
<a name="line337"></a>  }
<a name="line338"></a>
<a name="line339"></a>  return node;
<a name="line340"></a>};
<a name="line341"></a>
<a name="line342"></a>
<a name="line343"></a>/** @inheritDoc */
<a name="line344"></a>goog.dom.browserrange.IeRange.prototype.getStartNode = function() {
<a name="line345"></a>  return this.startNode_ ||
<a name="line346"></a>      (this.startNode_ = this.getEndpointNode_(goog.dom.RangeEndpoint.START));
<a name="line347"></a>};
<a name="line348"></a>
<a name="line349"></a>
<a name="line350"></a>/** @inheritDoc */
<a name="line351"></a>goog.dom.browserrange.IeRange.prototype.getStartOffset = function() {
<a name="line352"></a>  return this.getOffset_(goog.dom.RangeEndpoint.START);
<a name="line353"></a>};
<a name="line354"></a>
<a name="line355"></a>
<a name="line356"></a>/** @inheritDoc */
<a name="line357"></a>goog.dom.browserrange.IeRange.prototype.getEndNode = function() {
<a name="line358"></a>  return this.endNode_ ||
<a name="line359"></a>      (this.endNode_ = this.getEndpointNode_(goog.dom.RangeEndpoint.END));
<a name="line360"></a>};
<a name="line361"></a>
<a name="line362"></a>
<a name="line363"></a>/** @inheritDoc */
<a name="line364"></a>goog.dom.browserrange.IeRange.prototype.getEndOffset = function() {
<a name="line365"></a>  return this.getOffset_(goog.dom.RangeEndpoint.END);
<a name="line366"></a>};
<a name="line367"></a>
<a name="line368"></a>
<a name="line369"></a>/** @inheritDoc */
<a name="line370"></a>goog.dom.browserrange.IeRange.prototype.containsRange = function(range,
<a name="line371"></a>    opt_allowPartial) {
<a name="line372"></a>  return this.containsBrowserRange(range.range_, opt_allowPartial);
<a name="line373"></a>};
<a name="line374"></a>
<a name="line375"></a>
<a name="line376"></a>/** @inheritDoc */
<a name="line377"></a>goog.dom.browserrange.IeRange.prototype.compareBrowserRangeEndpoints =
<a name="line378"></a>    function(range, thisEndpoint, otherEndpoint) {
<a name="line379"></a>  return this.range_.compareEndPoints(
<a name="line380"></a>      (thisEndpoint == goog.dom.RangeEndpoint.START ? &#39;Start&#39; : &#39;End&#39;) +
<a name="line381"></a>      &#39;To&#39; +
<a name="line382"></a>      (otherEndpoint == goog.dom.RangeEndpoint.START ? &#39;Start&#39; : &#39;End&#39;),
<a name="line383"></a>      range);
<a name="line384"></a>};
<a name="line385"></a>
<a name="line386"></a>
<a name="line387"></a>/**
<a name="line388"></a> * Recurses to find the correct node for the given endpoint.
<a name="line389"></a> * @param {goog.dom.RangeEndpoint} endpoint The endpoint to get the node for.
<a name="line390"></a> * @param {Node} opt_node Optional node to start the search from.
<a name="line391"></a> * @return {Node} The deepest node containing the endpoint.
<a name="line392"></a> * @private
<a name="line393"></a> */
<a name="line394"></a>goog.dom.browserrange.IeRange.prototype.getEndpointNode_ = function(endpoint,
<a name="line395"></a>    opt_node) {
<a name="line396"></a>
<a name="line397"></a>  /** @type {Node} */
<a name="line398"></a>  var node = opt_node || this.getContainer();
<a name="line399"></a>
<a name="line400"></a>  // If we&#39;re at a leaf in the DOM, we&#39;re done.
<a name="line401"></a>  if (!node || !node.firstChild) {
<a name="line402"></a>    // Special case to ensure we match W3c behavior at BR edges.
<a name="line403"></a>    if (endpoint == goog.dom.RangeEndpoint.END &amp;&amp; node.previousSibling &amp;&amp;
<a name="line404"></a>        node.previousSibling.tagName == goog.dom.TagName.BR &amp;&amp;
<a name="line405"></a>        this.getOffset_(endpoint, node) == 0) {
<a name="line406"></a>      node = node.previousSibling;
<a name="line407"></a>    }
<a name="line408"></a>
<a name="line409"></a>    // To be consistent with other browsers, don&#39;t return BRs.
<a name="line410"></a>    return node.tagName == &#39;BR&#39; ? node.parentNode : node;
<a name="line411"></a>  }
<a name="line412"></a>
<a name="line413"></a>  // Find the first/last child that overlaps the selection
<a name="line414"></a>  var child = endpoint == goog.dom.RangeEndpoint.START ?
<a name="line415"></a>      node.firstChild : node.lastChild;
<a name="line416"></a>
<a name="line417"></a>  while (child) {
<a name="line418"></a>    if (this.containsNode(child, true)) {
<a name="line419"></a>      // If this child overlaps the selection, recurse down to find the
<a name="line420"></a>      // first/last child the next level down that overlaps the selection.
<a name="line421"></a>      return this.getEndpointNode_(endpoint, child);
<a name="line422"></a>    }
<a name="line423"></a>
<a name="line424"></a>    child = endpoint == goog.dom.RangeEndpoint.START ?
<a name="line425"></a>        child.nextSibling : child.previousSibling;
<a name="line426"></a>  }
<a name="line427"></a>
<a name="line428"></a>  // None of the children of this node overlapped the selection, that means
<a name="line429"></a>  // the selection starts/ends in this node directly.
<a name="line430"></a>  return node;
<a name="line431"></a>};
<a name="line432"></a>
<a name="line433"></a>
<a name="line434"></a>/**
<a name="line435"></a> * Returns the offset into the start/end container.
<a name="line436"></a> * @param {goog.dom.RangeEndpoint} endpoint The endpoint to get the offset for.
<a name="line437"></a> * @param {Node} opt_container The container to get the offset relative to.
<a name="line438"></a> *     Defaults to the value returned by getStartNode/getEndNode.
<a name="line439"></a> * @return {number} The offset.
<a name="line440"></a> * @private
<a name="line441"></a> */
<a name="line442"></a>goog.dom.browserrange.IeRange.prototype.getOffset_ = function(endpoint,
<a name="line443"></a>    opt_container) {
<a name="line444"></a>  var container = opt_container || (endpoint == goog.dom.RangeEndpoint.START ?
<a name="line445"></a>      this.getStartNode() : this.getEndNode());
<a name="line446"></a>
<a name="line447"></a>  if (container.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line448"></a>    // Find the first/last child that overlaps the selection
<a name="line449"></a>    var children = container.childNodes;
<a name="line450"></a>    var len = children.length;
<a name="line451"></a>    var i = endpoint == goog.dom.RangeEndpoint.START ? 0 : len - 1;
<a name="line452"></a>
<a name="line453"></a>    // We find the index in the child array of the endpoint of the selection.
<a name="line454"></a>    while (i &gt;= 0 &amp;&amp; i &lt; len) {
<a name="line455"></a>      var child = children[i];
<a name="line456"></a>
<a name="line457"></a>      // Stop looping when we reach the edge of the selection.
<a name="line458"></a>      if (this.containsNode(child, true)) {
<a name="line459"></a>        // Special case to ensure we match W3c behavior at BR edges.
<a name="line460"></a>        if (endpoint == goog.dom.RangeEndpoint.END &amp;&amp; child.previousSibling &amp;&amp;
<a name="line461"></a>            child.previousSibling.tagName == goog.dom.TagName.BR &amp;&amp;
<a name="line462"></a>            this.getOffset_(endpoint, child) == 0) {
<a name="line463"></a>          i--;
<a name="line464"></a>        }
<a name="line465"></a>
<a name="line466"></a>        break;
<a name="line467"></a>      }
<a name="line468"></a>
<a name="line469"></a>      // Update the child and the index.
<a name="line470"></a>      i += endpoint == goog.dom.RangeEndpoint.START ? 1 : -1;
<a name="line471"></a>    }
<a name="line472"></a>
<a name="line473"></a>    // When starting from the end in an empty container, we erroneously return
<a name="line474"></a>    // -1: fix this to return 0.
<a name="line475"></a>    return i == -1 ? 0 : i;
<a name="line476"></a>  } else {
<a name="line477"></a>    // Get a temporary range object.
<a name="line478"></a>    var range = this.range_.duplicate();
<a name="line479"></a>
<a name="line480"></a>    // Create a range that selects the entire container.
<a name="line481"></a>    var nodeRange = goog.dom.browserrange.IeRange.getBrowserRangeForNode_(
<a name="line482"></a>        container);
<a name="line483"></a>
<a name="line484"></a>    // Now, intersect our range with the container range - this should give us
<a name="line485"></a>    // the part of our selection that is in the container.
<a name="line486"></a>    range.setEndPoint(endpoint == goog.dom.RangeEndpoint.START ? &#39;EndToEnd&#39; :
<a name="line487"></a>                      &#39;StartToStart&#39;, nodeRange);
<a name="line488"></a>
<a name="line489"></a>    var rangeLength = range.text.length;
<a name="line490"></a>    return endpoint == goog.dom.RangeEndpoint.END ? rangeLength :
<a name="line491"></a>           container.length - rangeLength;
<a name="line492"></a>  }
<a name="line493"></a>};
<a name="line494"></a>
<a name="line495"></a>
<a name="line496"></a>/**
<a name="line497"></a> * Returns the text of the given node.  Uses IE specific properties.
<a name="line498"></a> * @param {Node} node The node to retrieve the text of.
<a name="line499"></a> * @return {string} The node&#39;s text.
<a name="line500"></a> * @private
<a name="line501"></a> */
<a name="line502"></a>goog.dom.browserrange.IeRange.getNodeText_ = function(node) {
<a name="line503"></a>  return node.nodeType == goog.dom.NodeType.TEXT ?
<a name="line504"></a>             node.nodeValue : node.innerText;
<a name="line505"></a>};
<a name="line506"></a>
<a name="line507"></a>
<a name="line508"></a>/**
<a name="line509"></a> * Tests whether this range is valid (i.e. whether its endpoints are still in
<a name="line510"></a> * the document).  A range becomes invalid when, after this object was created,
<a name="line511"></a> * either one or both of its endpoints are removed from the document.  Use of
<a name="line512"></a> * an invalid range can lead to runtime errors, particularly in IE.
<a name="line513"></a> * @return {boolean} Whether the range is valid.
<a name="line514"></a> */
<a name="line515"></a>goog.dom.browserrange.IeRange.prototype.isRangeInDocument = function() {
<a name="line516"></a>  var range = this.doc_.body.createTextRange();
<a name="line517"></a>  range.moveToElementText(this.doc_.body);
<a name="line518"></a>
<a name="line519"></a>  return this.containsBrowserRange(range, true);
<a name="line520"></a>};
<a name="line521"></a>
<a name="line522"></a>
<a name="line523"></a>/** @inheritDoc */
<a name="line524"></a>goog.dom.browserrange.IeRange.prototype.isCollapsed = function() {
<a name="line525"></a>  return this.range_.text == &#39;&#39;;
<a name="line526"></a>};
<a name="line527"></a>
<a name="line528"></a>
<a name="line529"></a>/** @inheritDoc */
<a name="line530"></a>goog.dom.browserrange.IeRange.prototype.getText = function() {
<a name="line531"></a>  return this.range_.text;
<a name="line532"></a>};
<a name="line533"></a>
<a name="line534"></a>
<a name="line535"></a>/** @inheritDoc */
<a name="line536"></a>goog.dom.browserrange.IeRange.prototype.getValidHtml = function() {
<a name="line537"></a>  return this.range_.htmlText;
<a name="line538"></a>};
<a name="line539"></a>
<a name="line540"></a>
<a name="line541"></a>// SELECTION MODIFICATION
<a name="line542"></a>
<a name="line543"></a>
<a name="line544"></a>/** @inheritDoc */
<a name="line545"></a>goog.dom.browserrange.IeRange.prototype.select = function(opt_reverse) {
<a name="line546"></a>  // IE doesn&#39;t support programmatic reversed selections.
<a name="line547"></a>  this.range_.select();
<a name="line548"></a>};
<a name="line549"></a>
<a name="line550"></a>
<a name="line551"></a>/** @inheritDoc */
<a name="line552"></a>goog.dom.browserrange.IeRange.prototype.removeContents = function() {
<a name="line553"></a>  if (this.range_.htmlText) {
<a name="line554"></a>    // Store some before-removal state.
<a name="line555"></a>    var startNode = this.getStartNode();
<a name="line556"></a>    var endNode = this.getEndNode();
<a name="line557"></a>    var oldText = this.range_.text;
<a name="line558"></a>
<a name="line559"></a>    // IE sometimes deletes nodes unrelated to the selection.  This trick fixes
<a name="line560"></a>    // that problem most of the time.  Even though it looks like a no-op, it is
<a name="line561"></a>    // somehow changing IE&#39;s internal state such that empty unrelated nodes are
<a name="line562"></a>    // no longer deleted.
<a name="line563"></a>    var clone = this.range_.duplicate();
<a name="line564"></a>    clone.moveStart(&#39;character&#39;, 1);
<a name="line565"></a>    clone.moveStart(&#39;character&#39;, -1);
<a name="line566"></a>
<a name="line567"></a>    // However, sometimes when the range is empty, moving the start back and
<a name="line568"></a>    // forth ends up changing the range.  This indicates a case we need to
<a name="line569"></a>    // handle manually.
<a name="line570"></a>    if (clone.text != oldText) {
<a name="line571"></a>      // Delete all nodes entirely contained in the range.
<a name="line572"></a>      var iter = new goog.dom.NodeIterator(startNode, false, true);
<a name="line573"></a>      var toDelete = [];
<a name="line574"></a>      goog.iter.forEach(iter, function(node) {
<a name="line575"></a>        // Any text node we encounter here is by definition contained entirely
<a name="line576"></a>        // in the range.
<a name="line577"></a>        if (node.nodeType != goog.dom.NodeType.TEXT &amp;&amp;
<a name="line578"></a>            this.containsNode(node)) {
<a name="line579"></a>          toDelete.push(node);
<a name="line580"></a>          iter.skipTag();
<a name="line581"></a>        }
<a name="line582"></a>        if (node == endNode) {
<a name="line583"></a>          throw goog.iter.StopIteration;
<a name="line584"></a>        }
<a name="line585"></a>      });
<a name="line586"></a>      this.collapse(true);
<a name="line587"></a>      goog.array.forEach(toDelete, goog.dom.removeNode);
<a name="line588"></a>
<a name="line589"></a>      this.clearCachedValues_();
<a name="line590"></a>      return;
<a name="line591"></a>    }
<a name="line592"></a>
<a name="line593"></a>    // Outside of the unfortunate cases where we have to handle deletion
<a name="line594"></a>    // manually, we can use the browser&#39;s native deletion code.
<a name="line595"></a>    this.range_ = clone;
<a name="line596"></a>    this.range_.text = &#39;&#39;;
<a name="line597"></a>    this.clearCachedValues_();
<a name="line598"></a>
<a name="line599"></a>    // Unfortunately, when deleting a portion of a single text node, IE creates
<a name="line600"></a>    // an extra text node unlike other browsers which just change the text in
<a name="line601"></a>    // the node.  We normalize for that behavior here, making IE behave like all
<a name="line602"></a>    // the other browsers.
<a name="line603"></a>    var newStartNode = this.getStartNode();
<a name="line604"></a>    var newStartOffset = this.getStartOffset();
<a name="line605"></a>    /** @preserveTry */
<a name="line606"></a>    try {
<a name="line607"></a>      var sibling = startNode.nextSibling;
<a name="line608"></a>      if (startNode == endNode &amp;&amp; startNode.parentNode &amp;&amp;
<a name="line609"></a>          startNode.nodeType == goog.dom.NodeType.TEXT &amp;&amp;
<a name="line610"></a>          sibling &amp;&amp; sibling.nodeType == goog.dom.NodeType.TEXT) {
<a name="line611"></a>        startNode.nodeValue += sibling.nodeValue;
<a name="line612"></a>        goog.dom.removeNode(sibling);
<a name="line613"></a>
<a name="line614"></a>        // Make sure to reselect the appropriate position.
<a name="line615"></a>        this.range_ = goog.dom.browserrange.IeRange.getBrowserRangeForNode_(
<a name="line616"></a>            newStartNode);
<a name="line617"></a>        this.range_.move(&#39;character&#39;, newStartOffset);
<a name="line618"></a>        this.clearCachedValues_();
<a name="line619"></a>      }
<a name="line620"></a>    } catch (e) {
<a name="line621"></a>      // IE throws errors on orphaned nodes.
<a name="line622"></a>    }
<a name="line623"></a>  }
<a name="line624"></a>};
<a name="line625"></a>
<a name="line626"></a>/**
<a name="line627"></a> * @param {TextRange} range The range to get a dom helper for.
<a name="line628"></a> * @return {goog.dom.DomHelper} A dom helper for the document the range
<a name="line629"></a> *     resides in.
<a name="line630"></a> * @private
<a name="line631"></a> */
<a name="line632"></a>goog.dom.browserrange.IeRange.getDomHelper_ = function(range) {
<a name="line633"></a>  return goog.dom.getDomHelper(range.parentElement());
<a name="line634"></a>};
<a name="line635"></a>
<a name="line636"></a>
<a name="line637"></a>/**
<a name="line638"></a> * Pastes the given element into the given range, returning the resulting
<a name="line639"></a> * element.
<a name="line640"></a> * @param {TextRange} range The range to paste into.
<a name="line641"></a> * @param {Element} element The node to insert a copy of.
<a name="line642"></a> * @param {goog.dom.DomHelper} opt_domHelper DOM helper object for the document
<a name="line643"></a> *     the range resides in.
<a name="line644"></a> * @return {Element} The resulting copy of element.
<a name="line645"></a> * @private
<a name="line646"></a> */
<a name="line647"></a>goog.dom.browserrange.IeRange.pasteElement_ = function(range, element,
<a name="line648"></a>    opt_domHelper) {
<a name="line649"></a>  opt_domHelper = opt_domHelper || goog.dom.browserrange.IeRange.getDomHelper_(
<a name="line650"></a>      range);
<a name="line651"></a>
<a name="line652"></a>  // Make sure the node has a unique id.
<a name="line653"></a>  var id;
<a name="line654"></a>  var originalId = id = element.id;
<a name="line655"></a>  if (!id) {
<a name="line656"></a>    id = element.id = goog.string.createUniqueString();
<a name="line657"></a>  }
<a name="line658"></a>
<a name="line659"></a>  // Insert (a clone of) the node.
<a name="line660"></a>  range.pasteHTML(element.outerHTML);
<a name="line661"></a>
<a name="line662"></a>  // Pasting the outerHTML of the modified element into the document creates
<a name="line663"></a>  // a clone of the element argument.  We want to return a reference to the
<a name="line664"></a>  // clone, not the original.  However we need to remove the temporary ID
<a name="line665"></a>  // first.
<a name="line666"></a>  element = opt_domHelper.$(id);
<a name="line667"></a>
<a name="line668"></a>  // If element is null here, we failed.
<a name="line669"></a>  if (element) {
<a name="line670"></a>    if (!originalId) {
<a name="line671"></a>      element.removeAttribute(&#39;id&#39;);
<a name="line672"></a>    }
<a name="line673"></a>  }
<a name="line674"></a>
<a name="line675"></a>  return element;
<a name="line676"></a>};
<a name="line677"></a>
<a name="line678"></a>
<a name="line679"></a>/** @inheritDoc */
<a name="line680"></a>goog.dom.browserrange.IeRange.prototype.surroundContents = function(element) {
<a name="line681"></a>  // Make sure the element is detached from the document.
<a name="line682"></a>  goog.dom.removeNode(element);
<a name="line683"></a>
<a name="line684"></a>  // IE more or less guarantees that range.htmlText is well-formed &amp; valid.
<a name="line685"></a>  element.innerHTML = this.range_.htmlText;
<a name="line686"></a>  element = goog.dom.browserrange.IeRange.pasteElement_(this.range_, element);
<a name="line687"></a>
<a name="line688"></a>  // If element is null here, we failed.
<a name="line689"></a>  if (element) {
<a name="line690"></a>    this.range_.moveToElementText(element);
<a name="line691"></a>  }
<a name="line692"></a>
<a name="line693"></a>  this.clearCachedValues_();
<a name="line694"></a>
<a name="line695"></a>  return element;
<a name="line696"></a>};
<a name="line697"></a>
<a name="line698"></a>
<a name="line699"></a>/**
<a name="line700"></a> * Internal handler for inserting a node.
<a name="line701"></a> * @param {TextRange} clone A clone of this range&#39;s browser range object.
<a name="line702"></a> * @param {Node} node The node to insert.
<a name="line703"></a> * @param {boolean} before Whether to insert the node before or after the range.
<a name="line704"></a> * @param {goog.dom.DomHelper} opt_domHelper The dom helper to use.
<a name="line705"></a> * @return {Node} The resulting copy of node.
<a name="line706"></a> * @private
<a name="line707"></a> */
<a name="line708"></a>goog.dom.browserrange.IeRange.insertNode_ = function(clone, node,
<a name="line709"></a>    before, opt_domHelper) {
<a name="line710"></a>  // Get a DOM helper.
<a name="line711"></a>  opt_domHelper = opt_domHelper || goog.dom.browserrange.IeRange.getDomHelper_(
<a name="line712"></a>      clone);
<a name="line713"></a>
<a name="line714"></a> // If it&#39;s not an element, wrap it in one.
<a name="line715"></a>  var isNonElement;
<a name="line716"></a>  if (node.nodeType != goog.dom.NodeType.ELEMENT) {
<a name="line717"></a>    isNonElement = true;
<a name="line718"></a>    node = opt_domHelper.createDom(goog.dom.TagName.DIV, null, node);
<a name="line719"></a>  }
<a name="line720"></a>
<a name="line721"></a>  clone.collapse(before);
<a name="line722"></a>  node = goog.dom.browserrange.IeRange.pasteElement_(clone,
<a name="line723"></a>      /** @type {Element} */ (node), opt_domHelper);
<a name="line724"></a>
<a name="line725"></a>  // If we didn&#39;t want an element, unwrap the element and return the node.
<a name="line726"></a>  if (isNonElement) {
<a name="line727"></a>    // pasteElement_() may have returned a copy of the wrapper div, and the
<a name="line728"></a>    // node it wraps could also be a new copy. So we must extract that new
<a name="line729"></a>    // node from the new wrapper.
<a name="line730"></a>    var newNonElement = node.firstChild;
<a name="line731"></a>    opt_domHelper.flattenElement(node);
<a name="line732"></a>    node = newNonElement;
<a name="line733"></a>  }
<a name="line734"></a>
<a name="line735"></a>  return node;
<a name="line736"></a>};
<a name="line737"></a>
<a name="line738"></a>
<a name="line739"></a>/** @inheritDoc */
<a name="line740"></a>goog.dom.browserrange.IeRange.prototype.insertNode = function(node, before) {
<a name="line741"></a>  var output = goog.dom.browserrange.IeRange.insertNode_(
<a name="line742"></a>      this.range_.duplicate(), node, before);
<a name="line743"></a>  this.clearCachedValues_();
<a name="line744"></a>  return output;
<a name="line745"></a>};
<a name="line746"></a>
<a name="line747"></a>
<a name="line748"></a>/** @inheritDoc */
<a name="line749"></a>goog.dom.browserrange.IeRange.prototype.surroundWithNodes = function(
<a name="line750"></a>    startNode, endNode) {
<a name="line751"></a>  var clone1 = this.range_.duplicate();
<a name="line752"></a>  var clone2 = this.range_.duplicate();
<a name="line753"></a>  goog.dom.browserrange.IeRange.insertNode_(clone1, startNode, true);
<a name="line754"></a>  goog.dom.browserrange.IeRange.insertNode_(clone2, endNode, false);
<a name="line755"></a>
<a name="line756"></a>  this.clearCachedValues_();
<a name="line757"></a>};
<a name="line758"></a>
<a name="line759"></a>
<a name="line760"></a>/** @inheritDoc */
<a name="line761"></a>goog.dom.browserrange.IeRange.prototype.collapse = function(toStart) {
<a name="line762"></a>  this.range_.collapse(toStart);
<a name="line763"></a>
<a name="line764"></a>  if (toStart) {
<a name="line765"></a>    this.endNode_ = this.startNode_;
<a name="line766"></a>  } else {
<a name="line767"></a>    this.startNode_ = this.endNode_;
<a name="line768"></a>  }
<a name="line769"></a>};
</pre>


</body>
</html>
